package com.hikeen.launcherx.floatwindow;

import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.tv.TvContract;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hikeen.launcherx.interfaces.MainListener;
import com.hikeen.launcherx.ui.MainActivity3;
import com.hjq.toast.ToastUtils;
import com.hikeen.launcherx.R;

/**
 * Author by LY
 * Email 1078410438@qq.com
 * Date on 2022/3/5.
 */
public class LRWindowManagers {

    private static LRWindowManagers instance;
    private Context mContext;
    private static String TAG = "PreViewManager";

    //定义浮动窗口布局
    private LinearLayout mFloatLayout_l;
    private LinearLayout mFloatLayout_input_l;
    private LinearLayout mFloatLayout_r;
    private LinearLayout mFloatLayout_input_r;
    //创建浮动窗口设置布局参数的对象
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams layoutParams;
    private static ImageView mFloatView_l;
    private static ImageView mFloatView_r;

    /**
     * 左侧
     */

    private static LinearLayout ll_gongju;
    private static LinearLayout lefta;
    private ImageView sidebar_back_l;
    private LinearLayout sidebar_btn1_l;
    ;
    private LinearLayout sidebar_btn2_l;
    ;
    private LinearLayout sidebar_btn3_l;
    ;
    private LinearLayout sidebar_btn4_l;
    ;
    private LinearLayout sidebar_btn5_l;
    ;
    private LinearLayout sidebar_btn6_l;
    ;
    private LinearLayout sidebar_btn7_l;
    ;
    private LinearLayout sidebar_btn8_l;
    ;

    private Intent intent;
    private static LinearLayout ll_shuru;
    ;
    private TextView tv_sr1_l;
    ;
    private TextView tv_sr2_l;
    ;
    private TextView tv_sr3_l;
    ;
    private TextView tv_sr4_l;
    ;
    private TextView tv_sr5_l;
    ;
    private TextView tv_sr6_l;
    ;

    /**
     * 右侧
     */
    private static LinearLayout rr_gongju;
    private ImageView sidebar_back_r;
    private LinearLayout sidebar_btn1_r;
    private LinearLayout sidebar_btn2_r;
    private LinearLayout sidebar_btn3_r;
    private LinearLayout sidebar_btn4_r;
    private LinearLayout sidebar_btn5_r;
    private LinearLayout sidebar_btn6_r;
    private LinearLayout sidebar_btn7_r;
    private LinearLayout sidebar_btn8_r;

    private static LinearLayout rr_shuru;
    private TextView tv_sr1_r;
    private TextView tv_sr2_r;
    private TextView tv_sr3_r;
    private TextView tv_sr4_r;
    private TextView tv_sr5_r;
    private TextView tv_sr6_r;
    private FloatWindowBuilder floatWindowBuilderLeft;
    private int height;
    private int width;
    private FloatWindowBuilder floatWindowBuilderRight;
    private FloatWindowBuilder floatWindowBuilderLeftInput;
    private FloatWindowBuilder floatWindowBuilderRightInput;

    private LRWindowManagers() {
    }

    public static LRWindowManagers getInstance() {
        if (instance == null) {
            instance = new LRWindowManagers();
        }
        return instance;
    }

    public void init(Context context) {
        mContext = context;

        floatWindowBuilderLeft = new FloatWindowBuilder.MyBuild().setContext(context)
                .setGravity(Gravity.LEFT, Gravity.TOP)
                .build();
        initButton_Left(floatWindowBuilderLeft.getWindowManager(), floatWindowBuilderLeft.getLayoutParams());

        floatWindowBuilderLeftInput = new FloatWindowBuilder.MyBuild().setContext(context)
                .setGravity(Gravity.LEFT, Gravity.TOP)
                .setSize(dip2px(mContext, 90), dip2px(mContext, 100))
                .setPosition(100, 600)
                .build();
        initButton_Left_input(floatWindowBuilderLeftInput.getWindowManager(), floatWindowBuilderLeftInput.getLayoutParams());

        floatWindowBuilderRight = new FloatWindowBuilder.MyBuild().setContext(context)
                .setGravity(Gravity.RIGHT, Gravity.TOP)
                .build();
        initButton_Right(floatWindowBuilderRight.getWindowManager(), floatWindowBuilderRight.getLayoutParams());
        DisplayMetrics dm = new DisplayMetrics();
        floatWindowBuilderLeft.getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        floatWindowBuilderRightInput = new FloatWindowBuilder.MyBuild().setContext(context)
                .setGravity(Gravity.LEFT, Gravity.TOP)
                .setSize(dip2px(mContext, 90), dip2px(mContext, 100))
                .setPosition(1640, 600)
                .build();

        initButton_Right_input(floatWindowBuilderRightInput.getWindowManager(), floatWindowBuilderRightInput.getLayoutParams());


    }

    //左侧
    public void initButton_Left(WindowManager mWindowManager, WindowManager.LayoutParams layoutParams) {

        //获取浮动窗口视图所在布局
        mFloatLayout_l = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_float_window, null);
        //添加mFloatLayout
        mWindowManager.addView(mFloatLayout_l, layoutParams);
        //浮动窗口按钮
        mFloatView_l = (ImageView) mFloatLayout_l.findViewById(R.id.iv_close_notify);

        initView_Left();

        mFloatLayout_l.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        //设置监听浮动窗口的触摸移动
//        mFloatView.setOnTouchListener(new View.OnTouchListener()
//        {
//            float x = 0,y = 0;
//
//            @Override
//
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        isClick = false;
//                        x = event.getRawX();
//                        y = event.getRawY();
//                        L.e("Https", "------ACTION_DOWN---x="+x+"---y="+y);
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        //Log.d(TAG, "------ACTION_MOVE---x="+event.getRawX()+"---y="+event.getRawY());
//                        if ((x - event.getRawX()) > 20
//                                || (event.getRawX() - x) > 20
//                                || (y - event.getRawY()) > 20
//                                || (event.getRawY() - y) > 20
//                        ) {
//                            L.e("Https", "------ACTION_MOVE");
//                            isClick = true;
//                            // getRawX是触摸位置相对于屏幕的坐标，getX是相对于按钮的坐标
//                            wmParams.x = (int) event.getRawX()
//                                    - mFloatView.getMeasuredWidth() / 2;
//                            // 减25为状态栏的高度
//                            wmParams.y = (int) event.getRawY()
//                                    - mFloatView.getMeasuredHeight() / 2 - 75;
//                            // 刷新
//                            mWindowManager.updateViewLayout(mFloatLayout, wmParams);
//                            return true;
//                        }
//                    case MotionEvent.ACTION_UP:
//                        L.e("Https", "------ACTION_UP");
//                        return isClick;// 此处返回false则属于移动事件，返回true则释放事件，可以出发点击否。
//                    default:
//                        break;
//                }
//                return false;
//            }
//        });
        mFloatView_l.setOnTouchListener(new FloatingListenerLEFT());
        ll_gongju.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "onTouch: 313   -----");

                return false;
            }
        });
        mFloatView_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: --LEFT  ");
                //TODO home
//                performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                performGlobalAction(AccessibilityNodeInfo.ACTION_CLICK);

                mFloatView_l.setVisibility(View.GONE);
                Log.d(TAG, "onClick: 123456");
                ll_gongju.setVisibility(View.VISIBLE);
                floatWindowBuilderLeft.getWindowManager().updateViewLayout(mFloatLayout_l, floatWindowBuilderLeft.getLayoutParams());

            }
        });

    }

    //左侧输入
    public void initButton_Left_input(WindowManager mWindowManager, WindowManager.LayoutParams layoutParams) {

        //获取浮动窗口视图所在布局
        mFloatLayout_input_l = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_float_input, null);
        //添加mFloatLayout
        mWindowManager.addView(mFloatLayout_input_l, layoutParams);


        mFloatLayout_input_l.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        tv_sr1_l = mFloatLayout_input_l.findViewById(R.id.tv_sr1);
        tv_sr2_l = mFloatLayout_input_l.findViewById(R.id.tv_sr2);
        tv_sr3_l = mFloatLayout_input_l.findViewById(R.id.tv_sr3);
        tv_sr4_l = mFloatLayout_input_l.findViewById(R.id.tv_sr4);
        tv_sr5_l = mFloatLayout_input_l.findViewById(R.id.tv_sr5);
        tv_sr6_l = mFloatLayout_input_l.findViewById(R.id.tv_sr6);

        tv_sr1_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI1));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });
        tv_sr2_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI2));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });
        tv_sr3_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI3));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });
        tv_sr4_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_YPP));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });
        tv_sr5_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_VGA));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });
        tv_sr6_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_AV));
                ll_shuru.setVisibility(View.GONE);
                initShuru(1);
            }
        });


    }

    //右侧
    public void initButton_Right(WindowManager mWindowManager, WindowManager.LayoutParams layoutParams) {

        //获取浮动窗口视图所在布局
        mFloatLayout_r = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_right_window, null);
        //添加mFloatLayout_r
        mWindowManager.addView(mFloatLayout_r, layoutParams);
        //浮动窗口按钮
        mFloatView_r = (ImageView) mFloatLayout_r.findViewById(R.id.iv_close_notify);

        initView_Right();

        mFloatLayout_r.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        设置监听浮动窗口的触摸移动
        mFloatView_r.setOnTouchListener(new FloatingListenerRight());

        mFloatView_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: --LEFT  ");
                //TODO home

                mFloatView_r.setVisibility(View.GONE);
                rr_gongju.setVisibility(View.VISIBLE);
                floatWindowBuilderLeft.getWindowManager().updateViewLayout(mFloatLayout_l, floatWindowBuilderLeft.getLayoutParams());

            }
        });

    }

    //右侧输入
    public void initButton_Right_input(WindowManager mWindowManager, WindowManager.LayoutParams layoutParams) {

        //获取浮动窗口视图所在布局
        mFloatLayout_input_r = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_float_input, null);
        //添加mFloatLayout
        mWindowManager.addView(mFloatLayout_input_r, layoutParams);


        mFloatLayout_input_r.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        tv_sr1_r = mFloatLayout_input_r.findViewById(R.id.tv_sr1);
        tv_sr2_r = mFloatLayout_input_r.findViewById(R.id.tv_sr2);
        tv_sr3_r = mFloatLayout_input_r.findViewById(R.id.tv_sr3);
        tv_sr4_r = mFloatLayout_input_r.findViewById(R.id.tv_sr4);
        tv_sr5_r = mFloatLayout_input_r.findViewById(R.id.tv_sr5);
        tv_sr6_r = mFloatLayout_input_r.findViewById(R.id.tv_sr6);

        tv_sr1_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI1));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);

            }
        });
        tv_sr2_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI2));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);
            }
        });
        tv_sr3_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_HDMI3));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);
            }
        });
        tv_sr4_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_YPP));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);
            }
        });
        tv_sr5_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_VGA));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);
            }
        });
        tv_sr6_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;
                startTvWithChannel(TvContract.buildChannelUriForPassthroughInput(Constant.SOURCE_AV));
                rr_shuru.setVisibility(View.GONE);
                initShuru(2);
            }
        });
    }

    private void initView_Left() {
        ll_gongju = mFloatLayout_l.findViewById(R.id.ll_gongju);
        sidebar_back_l = mFloatLayout_l.findViewById(R.id.sidebar_back);
        sidebar_btn1_l = mFloatLayout_l.findViewById(R.id.sidebar_btn1);
        sidebar_btn2_l = mFloatLayout_l.findViewById(R.id.sidebar_btn2);
        sidebar_btn3_l = mFloatLayout_l.findViewById(R.id.sidebar_btn3);
        sidebar_btn4_l = mFloatLayout_l.findViewById(R.id.sidebar_btn4);
        sidebar_btn5_l = mFloatLayout_l.findViewById(R.id.sidebar_btn5);
        sidebar_btn6_l = mFloatLayout_l.findViewById(R.id.sidebar_btn6);
        sidebar_btn7_l = mFloatLayout_l.findViewById(R.id.sidebar_btn7);
        sidebar_btn8_l = mFloatLayout_l.findViewById(R.id.sidebar_btn8);

        ll_shuru = mFloatLayout_l.findViewById(R.id.ll_shuru);


        sidebar_back_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ll_gongju.setVisibility(View.GONE);
                ll_shuru.setVisibility(View.GONE);
//                initShuru(1);
                mFloatLayout_input_l.findViewById(R.id.rr_shuru).setVisibility(View.GONE);
                mFloatView_l.setVisibility(View.VISIBLE);
                floatWindowBuilderLeft.getWindowManager().updateViewLayout(mFloatLayout_l, floatWindowBuilderLeft.getLayoutParams());

            }
        });

        sidebar_btn1_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: HOME");
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addCategory(Intent.CATEGORY_HOME);
                            mContext.startActivity(intent);
                        } catch (Exception e) {
                            Log.e("Exception when onBack", e.toString());
                        }
                    }
                }.start();
                mMainListener.reset();
            }
        });
        sidebar_btn2_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: BACK");
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            Log.e("Exception when onBack", e.toString());
                        }
                    }
                }.start();
//                mMainListener.reset();
            }
        });
        sidebar_btn3_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;

                Intent mIntent = new Intent();
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName component = new ComponentName("com.hikeen.mark", "com.hikeen.mark.MainActivity");
                mIntent.setComponent(component);
                mContext.startActivity(mIntent);
            }
        });
        sidebar_btn4_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                ComponentName components = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.common.CallService");
                intent.setComponent(components);
                intent.putExtra("key", "screenapp_start");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startService(intent);
                sidebar_back_l.performClick();
            }
        });
        sidebar_btn5_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("该功能暂未开放");
            }
        });
        sidebar_btn6_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMainListener.recent();
                intent = new Intent(mContext, MainActivity3.class);
                intent.putExtra("postion", 0);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
        sidebar_btn7_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("该功能暂未开放");
            }
        });
        sidebar_btn8_l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initShuru(1);
//                ll_shuru.setVisibility(ll_shuru.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            }


        });


    }

    private void initView_Right() {

        rr_gongju = mFloatLayout_r.findViewById(R.id.rr_gongju);
        sidebar_back_r = mFloatLayout_r.findViewById(R.id.sidebar_back);
        sidebar_btn1_r = mFloatLayout_r.findViewById(R.id.sidebar_btn1);
        sidebar_btn2_r = mFloatLayout_r.findViewById(R.id.sidebar_btn2);
        sidebar_btn3_r = mFloatLayout_r.findViewById(R.id.sidebar_btn3);
        sidebar_btn4_r = mFloatLayout_r.findViewById(R.id.sidebar_btn4);
        sidebar_btn5_r = mFloatLayout_r.findViewById(R.id.sidebar_btn5);
        sidebar_btn6_r = mFloatLayout_r.findViewById(R.id.sidebar_btn6);
        sidebar_btn7_r = mFloatLayout_r.findViewById(R.id.sidebar_btn7);
        sidebar_btn8_r = mFloatLayout_r.findViewById(R.id.sidebar_btn8);
        rr_shuru = mFloatLayout_r.findViewById(R.id.rr_shuru);
        sidebar_back_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rr_gongju.setVisibility(View.GONE);
                rr_shuru.setVisibility(View.GONE);
//                initShuru(2);
                mFloatLayout_input_r.findViewById(R.id.rr_shuru).setVisibility(View.GONE);
                mFloatView_r.setVisibility(View.VISIBLE);
                floatWindowBuilderRight.getWindowManager().updateViewLayout(mFloatLayout_l, floatWindowBuilderLeft.getLayoutParams());


            }
        });

        sidebar_btn1_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: HOME");
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addCategory(Intent.CATEGORY_HOME);
                            mContext.startActivity(intent);
                        } catch (Exception e) {
                            Log.e("Exception when onBack", e.toString());
                        }
                    }
                }.start();
                mMainListener.reset();
            }
        });
        sidebar_btn2_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: BACK");
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            Log.e("Exception when onBack", e.toString());
                        }
                    }
                }.start();
//                mMainListener.reset();
            }
        });
        sidebar_btn3_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity3.isShowOne = true;

                Intent mIntent = new Intent();
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName component = new ComponentName("com.hikeen.mark", "com.hikeen.mark.MainActivity");
                mIntent.setComponent(component);
                mContext.startActivity(mIntent);
            }
        });
        sidebar_btn4_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                ComponentName components = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.common.CallService");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(components);
                intent.putExtra("key", "screenapp_start");
                mContext.startService(intent);

                sidebar_back_r.performClick();
            }
        });
        sidebar_btn5_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("该功能暂未开放");
            }
        });
        sidebar_btn6_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMainListener.recent();
                intent = new Intent(mContext, MainActivity3.class);
                intent.putExtra("postion", 0);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
        sidebar_btn7_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("该功能暂未开放");
            }
        });
        sidebar_btn8_r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initShuru(2);
//                rr_shuru.setVisibility(rr_shuru.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);

//                ComponentName componentss = new ComponentName("com.hikeen.sidebar", "com.hikeen.sidebar.WindowService");
//                serviceIntent.setComponent(componentss);
//                serviceIntent.putExtra("key", "signalview_start");
//                mContext.startService(serviceIntent);
            }
        });


    }

    public void openwinds() {
        ll_gongju.setVisibility(View.VISIBLE);
        mFloatView_l.setVisibility(View.GONE);
//        ll_shuru.setVisibility(ll_shuru.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        initShuru(1);
    }

    /**
     * 2853切换通道
     *
     * @param
     */
    public void startTvWithChannel(Uri channelUri) {
        Intent intent = new Intent(Intent.ACTION_VIEW, channelUri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public static MainListener mMainListener;

    public static void setMainListener(MainListener mainListener) {
        mMainListener = mainListener;
    }


    //开始触控的坐标，移动时的坐标（相对于屏幕左上角的坐标）
    private int mTouchStartX, mTouchStartY, mTouchCurrentX, mTouchCurrentY;
    //开始时的坐标和结束时的坐标（相对于自身控件的坐标）
    private int mStartX, mStartY, mStopX, mStopY;
    private boolean isMove;

    /**
     * 悬浮窗监听器
     */
    private class FloatingListenerLEFT implements View.OnTouchListener {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            Log.d(TAG, "onTouch: ----------------------------------------");
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    isMove = false;
//                    mTouchStartX = (int) event.getRawX();
                    mTouchStartY = (int) event.getRawY();
                    mStartX = (int) event.getX();
                    mStartY = (int) event.getY();

                    break;
                case MotionEvent.ACTION_MOVE:
//                    mTouchCurrentX = (int) event.getRawX();
                    mTouchCurrentY = (int) event.getRawY();
//                    floatWindowBuilderLeft.getLayoutParams().x += mTouchCurrentX - mTouchStartX;
                    floatWindowBuilderLeft.getLayoutParams().y += mTouchCurrentY - mTouchStartY;
                    if (mFloatLayout_l != null) {
                        floatWindowBuilderLeft.getWindowManager().updateViewLayout(mFloatLayout_l, floatWindowBuilderLeft.getLayoutParams());
                    }
//                    mTouchStartX = mTouchCurrentX;
                    mTouchStartY = mTouchCurrentY;
                    break;
                case MotionEvent.ACTION_UP:
//                    mStopX = (int) event.getX();
                    mStopY = (int) event.getY();
                    if (Math.abs(mStartX - mStopX) == 1 || Math.abs(mStartY - mStopY) == 1) {
                        isMove = true;
                    }

                    break;
                default:
                    break;
            }
            return false; //此处必须返回false，否则OnClickListener获取不到监听
        }
    }

    private class FloatingListenerRight implements View.OnTouchListener {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            Log.d(TAG, "onTouch: ----------------------------------------");
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    isMove = false;
//                    mTouchStartX = (int) event.getRawX();
                    mTouchStartY = (int) event.getRawY();
                    mStartX = (int) event.getX();
                    mStartY = (int) event.getY();

                    break;
                case MotionEvent.ACTION_MOVE:
//                    mTouchCurrentX = (int) event.getRawX();
                    mTouchCurrentY = (int) event.getRawY();
//                    floatWindowBuilderRight.getLayoutParams().x += mTouchCurrentX - mTouchStartX;
                    floatWindowBuilderRight.getLayoutParams().y += mTouchCurrentY - mTouchStartY;
                    if (mFloatLayout_r != null) {
                        floatWindowBuilderRight.getWindowManager().updateViewLayout(mFloatLayout_r, floatWindowBuilderRight.getLayoutParams());
                    }
//                    mTouchStartX = mTouchCurrentX;
                    mTouchStartY = mTouchCurrentY;
                    break;
                case MotionEvent.ACTION_UP:
//                    mStopX = (int) event.getX();
                    mStopY = (int) event.getY();
                    if (Math.abs(mStartX - mStopX) == 1 || Math.abs(mStartY - mStopY) == 1) {
                        isMove = true;
                    }

                    break;
                default:
                    break;
            }
            return false; //此处必须返回false，否则OnClickListener获取不到监听
        }
    }


    private void initShuru(int flag) {
        Log.d(TAG, "initShuru: ");
        if (flag == 1) {
            mFloatLayout_input_l.findViewById(R.id.rr_shuru).setVisibility(mFloatLayout_input_l.findViewById(R.id.rr_shuru).getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
//            mFloatLayout_input_l.findViewById(R.id.rr_shuru).setVisibility(View.VISIBLE);
        } else {
            mFloatLayout_input_r.findViewById(R.id.rr_shuru).setVisibility(mFloatLayout_input_r.findViewById(R.id.rr_shuru).getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
//
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void onBoom() {

        floatWindowBuilderLeft.getWindowManager().removeView(mFloatLayout_l);
        floatWindowBuilderRight.getWindowManager().removeView(mFloatLayout_r);
        floatWindowBuilderLeftInput.getWindowManager().removeView(mFloatLayout_input_l);
        floatWindowBuilderRightInput.getWindowManager().removeView(mFloatLayout_input_r);
    }


}
